#!/usr/bin/env python
# -*- coding:utf8 -*-
"""
Author : mafei
Date   : 2019-12-07
"""
from apscheduler.schedulers.background import BackgroundScheduler
import time
import logging

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.memory import MemoryJobStore
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from sqlalchemy import create_engine
from mmonitor.common.config import mysql_db, mysql_password, mysql_server, mysql_user, mysql_port, rpc_port, rpc_host

mysql_url = 'mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8' % (mysql_user,
                                                             mysql_password,
                                                             mysql_server,
                                                             mysql_port,
                                                             mysql_db)
jobstores = {
    'default': SQLAlchemyJobStore(engine=create_engine(mysql_url,
                                                       encoding='utf-8',
                                                       pool_recycle=10,
                                                       echo=False))
}
scheduler = BackgroundScheduler(
    timezone='Asia/Shanghai',
    jobstores=jobstores,
    job_defaults=dict(misfire_grace_time=24 * 3600))


def job1(z):
    from mmonitor.models.mysql_db import TestModel
    TestModel.create(**{"msg":"%s: 执行任务: %s" % (time.asctime(), z)}).execute()
    print "%s: 执行任务: %s" % (time.asctime(), z)

from mmonitor.main.spider import run_job
# scheduler.add_job(job1, 'interval', seconds=3, args=["a"])
scheduler.start()
while True:
    res = scheduler.get_jobs()
    print(res)
    time.sleep(1)
# scheduler.start()